<%--
  Created by IntelliJ IDEA.
  User: 18318
  Date: 2022/6/26
  Time: 16:08
  To change this template use File | Settings | File Templates.
  title：用户管理页面
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<html>
<head>
    <title>SAS题目管理页面</title>
</head>
<script src="${pageContext.request.contextPath}/static/layui/layui.js"></script>
<link href="${pageContext.request.contextPath}/static/layui/css/layui.css" rel="stylesheet" media="all">
<body>
<%--添加页面--%>
<form class="layui-form" action="" id="insaertQT" style="display: none;margin-top: 20px;">
    <div class="layui-form-item" style="margin-top: 20px">
        <label class="layui-form-label">问题：</label>
        <div class="layui-input-block" style="width: 70%">
            <input id="Questiont" type="text" name="question" required lay-verify="required" placeholder="请输入角色名称"
                   autocomplete="off" class="layui-input" ><span style="color: red"></span>

        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-block" style="width: 70%">
            <input type="text" name="optiona" required lay-verify="required" placeholder="请输入角色权限字符串"
                   value="A 没有或者很少" autocomplete="off" class="layui-input"><span style="color: #ff0000"></span>

        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-block" style="width: 70%">
            <input type="text" name="optionb" required lay-verify="required" placeholder="请输入角色权限字符串"
                   value="B 有时候有" autocomplete="off" class="layui-input"><span style="color: red"></span>

        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-block" style="width: 70%">
            <input type="text" name="optionc" required lay-verify="required" placeholder="请输入角色权限字符串"
                   value="C 大部分时间有" autocomplete="off" class="layui-input"><span style="color: red"></span>

        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-block" style="width: 70%">
            <input type="text" name="optiond" required lay-verify="required" placeholder="请输入角色权限字符串"
                   value="D 绝大多数时间有" autocomplete="off" class="layui-input"><span style="color: red"></span>
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">类型</label>
        <div class="layui-input-block">
            <input type="radio" name="type" value="3" title="SAS正向题" checked="">
            <input type="radio" name="type" value="4" title="SAS反向题">
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="0" title="正常" checked="">
            <input type="radio" name="status" value="1" title="停用">
        </div>
    </div>
    <div class="layui-form-item" style="display: none" >
        <label class="layui-form-label">创建人</label>
        <div class="layui-input-block"  style="width: 70%">
            <input type="text" value="${userinfo.username}" name="createby" required lay-verify="required" placeholder="请输入创建者" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formQuseTion" id="addSub">添加</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>









<%--修改页面--%>
<form class="layui-form" id="updateQuesTion" style="display: none" lay-filter="updatequesTion"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-inline" style="display: none">
        <label class="layui-form-label">ID</label>
        <div id="QTID" class="layui-input-block" style=" width: 200px">
            <input type="text" name="id" placeholder="请输入" autocomplete="off" class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item" style="margin-top: 10px">
        <label class="layui-form-label">问题：</label>
        <div  class="layui-input-block" style="width: 70%">
            <input type="text" id="Questiong" name="question" required lay-verify="required" placeholder="请输入问题"
                   autocomplete="off" class="layui-input" ><span style="color: red"></span>

        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-block" style="width: 70%">
            <input type="text" name="optiona" required lay-verify="required" placeholder="请输入答案A"
                    autocomplete="off" class="layui-input"><span style="color: red"></span>
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-block" style="width: 70%">
            <input type="text" name="optionb" required lay-verify="required" placeholder="请输入答案B"
                    autocomplete="off" class="layui-input"><span style="color: red"></span>
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-block" style="width: 70%">
            <input type="text" name="optionc" required lay-verify="required" placeholder="请输入答案C"
                   autocomplete="off" class="layui-input"><span style="color: red"></span>
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-block" style="width: 70%">
            <input type="text" name="optiond" required lay-verify="required" placeholder="请输入答案D"
                    autocomplete="off" class="layui-input"><span style="color: red"></span>
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">类型</label>
        <div class="layui-input-block">
            <input type="radio" name="type" value="3" title="SAS正向题" checked="">
            <input type="radio" name="type" value="4" title="SDS反向题">
        </div>
    </div>

    <div class="layui-form-item" >
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="0" title="正常" checked="">
            <input type="radio" name="status" value="1" title="停用">
        </div>
    </div>
    <div class="layui-form-item" style="display: none">
        <label class="layui-form-label">修改人</label>
        <div class="layui-input-block"  style="width: 70%">
            <input type="text" value="${userinfo.username}" name="updateby" required lay-verify="required" placeholder="请输入创建者" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-input-block">
        <button type="button" class="layui-btn "  lay-submit lay-filter="updatequestion">修改</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form>
<%--自定义头部工具栏--%>
<script type="text/html" id="toolbar">
    <a class="layui-btn  layui-btn-sm" lay-event="add">添加</a>
    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="delete">批量删除</a>
</script>
<%--头部模糊查询--%>
<form class="layui-form" action="" style="margin-top: 10px">
    <div class="layui-inline">
        <label style=" width: 120px" class="layui-form-label">问题</label>
        <div class="layui-input-inline">
            <input type="text" name="question" placeholder="请输入问题" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">修改人</label>
        <div class="layui-input-inline">
            <input type="text" name="updateby" placeholder="请输入修改人" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="formDemo">查询</button>
            <button type="reset" id="search_reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<%--行内工具栏--%>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
</script>
<%--表格内容--%>
<table id="demo" lay-filter="test"></table>
</body>
<script>
    layui.use(['table', 'form', 'jquery', 'upload', 'element', 'layer'], function () {
        var table = layui.table,
            form = layui.form
            ,$ = layui.jquery
            , upload = layui.upload
            , element = layui.element
            , layer = layui.layer;
        //第一个实例
        table.render({
            elem: '#demo'
            , height: 500
            , toolbar: '#toolbar'//toolbar: '#toolbar'//开启顶部工具栏
            ,url: '${pageContext.request.contextPath}/QTSAS/FindAllQuestionSAS.do' //数据接口
            , page: true //开启分页
            , limit: 5
            , limits: [5, 10, 15]
            ,autoSort: true //禁用前端的自动排序，开启后端的服务器排序，根据客户需求
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', title: 'ID', width:70, sort: true, fixed: 'left'}
                ,{field: 'question', title: '问题', width:280,sort: true}
                ,{field: 'optiona', title: '选项A', width:130}
                ,{field: 'optionb', title: '选项B', width:130}
                ,{field: 'optionc', title: '选项C', width: 130}
                ,{field: 'optiond', title: '选项D', width: 130}
                ,{field: 'dictionaryname', title: '类型', width: 100,
                    templet: function (d) {
                        return d.dictionary.dictionaryname
                }
                }
                ,{field: 'status', title: '状态', width: 70,
                    templet: function (d) {
                        if (d.status == "0")
                            return '<button class="layui-btn layui-btn-xs">正常</button>';
                        else if (d.status == "1")
                            return '<button class="layui-btn layui-btn-danger layui-btn-xs">停用</button>';
                    }
                }
                ,{field: 'createby', title: '创建人', width:80}
                ,{field: 'createtime', title: '创建时间', width: 120}
                ,{field: 'updateby', title: '修改人', width: 80}
                ,{field: 'updatetime', title: '修改时间', width: 120}
                ,{fixed: 'right', title: '操作',width: 180, align:'center', toolbar: '#barDemo'}
            ]]
        });
         // 模糊查询  查询
        form.on('submit(formDemo)', function (data) {
            console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            //刷新表格数据
            table.reload('demo', {
                where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    question: data.field.question //排序字段
                    , updateby: data.field.updateby//排序方式
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                },
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //  模糊查询  重置
        $("#search_reset").click(function () {
            //刷新表格数据
            table.reload('demo', {
                where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    question: "" //第一个查询条件
                    , updateby: ""//第二个查询条件
                    // 把两个查询输入框置空，然后刷新一下
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                },
            });
        })

        //头部工具栏触发事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据;
            switch (obj.event) {
                case 'add':
                    layer.open({
                        title: '添加问题',
                        skin: "layui-layer-molv",//弹出层皮肤样式
                        type: 1,
                        content: $("#insaertQT"),
                        area: ['400px', '550px']
                    })

                    //鼠标离开输入框判断是否重名
                    $("#Questiont").blur(function () {
                        $.ajax({
                            url: '${pageContext.request.contextPath}/QTSAS/QuestionrepeatSAS.do',
                            data: {
                                question: $("#Questiont").val()
                            },
                            dataType: "json",
                            type: "get",
                            success: function (re) {
                                if (re.code == 0) {
                                    $("#Questiont").next().html("可以使用");
                                    $('#addSub').removeClass("layui-btn-disabled").attr("disabled", false);
                                } else {
                                    $("#Questiont").next().html("重名，不可使用");
                                    $('#addSub').addClass("layui-btn-disabled").attr("disabled", true);
                                }
                            }
                        })
                    })

                    //监听提交按钮
                    form.on('submit(formQuseTion)', function (data) {
                        // console.log(data)
                        //ajax方式提交表单数据，将数据保存至数据库中
                        $.ajax({
                            url: '${pageContext.request.contextPath}/QTSAS/InsaertQuestionSAS.do',
                            type: "post",
                            dataType: "JSON",
                            data: data.field,
                            success: function (ret) {
                                if (ret.code == 0) {
                                    //刷新网页缓存
                                    $("#insaertQT")[0].reset();
                                    layer.closeAll();//关闭对话框
                                    table.reload('demo', {
                                        page: {
                                            curr: 1 //重新从第 1 页开始
                                        }
                                    });
                                }
                                layer.msg(ret.msg);
                            }
                        });
                        table.reload('demo', {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                    });
                    break;
                //    批量删除
                case 'delete':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        var id=[]
                        for (var i=0;i<data.length;i++){
                            id[i]=data[i].id;
                        }
                        console.log(id.join(","))
                        layer.confirm('是否要全部干掉', function(index){
                            //确定后进入后台删除数据
                            $.ajax({
                                url: "${pageContext.request.contextPath}/QTSAS/deleteQuestionSAS.do",
                                data:{
                                    id:id.join(",")
                                },
                                dataType:"json",
                                type: "post",
                                success:function (a){
                                    if (a.code==0){
                                        //关闭所有对话框
                                        layer.closeAll();
                                        table.reload('demo',{
                                            page:{
                                                curr: 1
                                            }
                                        })
                                    }
                                }
                            })
                        })
                    }
                    break;
                }
            })





        //行内工具栏触发事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            // 删除
            if (layEvent === 'delete') {
                layer.confirm('是否干掉', function (index) {
                    console.log(data.roleid)
                    //删除数据库中
                    $.ajax({
                        url: "${pageContext.request.contextPath}/QTSAS/deleteQuestionSAS.do",
                        type: "POST",
                        dataType: "JSON",
                        data: {id: data.id},
                        success: function (data) {
                            if (data.code == 0) {
                                //关闭对话框
                                layer.closeAll(); //疯狂模式，关闭所有层
                                //刷新表格数据
                                table.reload('demo');

                            }
                            layer.msg(data.msg);
                        }

                    });
                });
            // 修改
            }else if (layEvent === 'edit'){
                console.log(data.id+"")
                layer.open({
                    title:"修改问题及答案",
                    type:1,
                    content:$("#updateQuesTion"),
                    area:['400px','550px']
                })
                form.val("updatequesTion", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                    "id": obj.data.id,// "name": "value"
                    "question": obj.data.question,
                    "optiona":obj.data.optiona,
                    "optionb":obj.data.optionb,
                    "optionc":obj.data.optionc,
                    "optiond":obj.data.optiond,
                    "type":obj.data.type,
                    "status":obj.data.status
                });
                form.on('submit(updatequestion)', function(data){
                    console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
                    console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
                    console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
                    $.ajax({
                        url:"${pageContext.request.contextPath}/QTSAS/updateQuestionSAS.do",
                        data:data.field,
                        dataType:"json",
                        type:"post",
                        success:function (a){
                            if (a.code==0){
                                layer.closeAll()
                                table.reload('demo')
                            }
                            layer.msg(a.msg)
                        }
                    })
                    return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                });
            }
         });
    });
</script>
</html>
